package com.yj_app.qualitytest.activity.login.presenter;

import android.app.Activity;
import android.util.Log;
import android.widget.Toast;


import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.yj_app.qualitytest.activity.login.contract.LoginContract;
import com.yj_app.qualitytest.activity.login.model.LoginModel;
import com.yj_app.qualitytest.base.mvp.BasePresenterImpl;
import com.yj_app.qualitytest.callback.DialogCallback;
import com.yj_app.qualitytest.common.Config;
import com.yj_app.qualitytest.info.BaseInfo;
import com.yj_app.qualitytest.info.CompanyInfo;
import com.yj_app.qualitytest.info.Loginfo;
import com.yj_app.qualitytest.info.up.MsgBean;
import com.yj_app.qualitytest.util.GsonUtil;
import com.yj_app.qualitytest.util.LogUtil;

import java.lang.reflect.Type;
import java.util.List;


public class LoginPresenter extends BasePresenterImpl<LoginContract.View,LoginContract.Model> implements LoginContract.Presenter {


    @Override
    protected LoginContract.Model createModel() {
        return new LoginModel();
    }

    @Override
    public void getCompanies() {
        MsgBean msgBean=new MsgBean();
        msgBean.setMethod(Config.METHOD_COMPANY);
        mModel.getCompanies2(msg2json(msgBean),new DialogCallback<String>((Activity) mView) {
            @Override
            public void onSuccess(Response<String> response) {
                String json=getMsgBean(response.body());
                Type type=new TypeToken<MsgBean<List<CompanyInfo>>>(){}.getType();
                MsgBean<List<CompanyInfo>> msgBean1 =GsonUtil.fromJson(json,type);
                mView.reCompaniesData(msgBean1.getMapList());

            }
        });


//        mModel.getCompanies(new DialogCallback<BaseInfo<List<CompanyInfo>>>((Activity) mView) {
//            @Override
//            public void onSuccess(Response<BaseInfo<List<CompanyInfo>>> response) {
//                mView.reCompaniesData(response.body().getList());
//            }
//        });
    }

    @Override
    public void login(String username, String userpwd) {
        MsgBean msgBean=new MsgBean();
        msgBean.setCode(username);
        msgBean.setPsw(userpwd);
        msgBean.setMethod(Config.METHOD_LOGIN);
        mModel.getLoginfo2(msg2json(msgBean), new DialogCallback<String>((Activity) mView) {
            @Override
            public void onSuccess(Response<String> response) {
                String json=getMsgBean(response.body());

                MsgBean msgBean1= GsonUtil.fromJson(json,MsgBean.class);
                Loginfo loginfo =new Loginfo();
                loginfo.setGuid(msgBean1.getGuid());
                loginfo.setSuccess(msgBean1.isSuccess());
                loginfo.setMsg(msgBean1.getMsg());
                loginfo.setNickname(msgBean1.getNickname());
                loginfo.setCode(msgBean1.getCode());
                mView.reLoginData(loginfo);

            }
        });




//        mModel.getLoginfo(username,userpwd, new DialogCallback<String>((Activity) mView) {
//            @Override
//            public void onSuccess(Response<String> response) {
//
//
//
//
//                mView.reLoginData(response.body());
//            }
//        });
    }


}
